package com.alibaba.citrus.ots.platform.enhance.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.ots.platform.enhance.service.ScItemUnitMeshFlowService;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.otsplatformenhance.api.unit.scitemunitquery.service.ScItemUnitQueryService;
import com.epoch.app.otsplatformenhance.api.unit.scitemunitwrite.service.ScItemUnitWriteService;
import com.epoch.app.otsplatformenhance.dto.request.CreateScItemUnitRequest;
import com.epoch.app.otsplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.otsplatformenhance.dto.request.QueryScItemUnitRequest;
import com.epoch.app.otsplatformenhance.dto.response.ScItemUnitResponse;
import com.epoch.app.otsplatformenhance.sdo.ScItemUnitSDO;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @author zls
 * @description: TODO
 * @date 2021/9/8 19:05
 */
@Primary
@Component
public class ScItemUnitMeshFlowServiceImpl implements ScItemUnitMeshFlowService {

    @Resource
    ScItemUnitQueryService scItemUnitQueryService;

    @Resource
    ScItemUnitWriteService scItemUnitWriteService;

    @Override
    public Result<String> createScItemUnit(CreateScItemUnitRequest request) {
        ScItemUnitSDO scItemUnitSDO = new ScItemUnitSDO();
        BeanUtils.copyProperties(request, scItemUnitSDO);
        return scItemUnitWriteService.createScItemUnit(scItemUnitSDO);
    }

    @Override
    public Result<ScItemUnitResponse> getScItemUnit(IdQueryRequest request) {
        return scItemUnitQueryService.getScItemUnit(request);
    }

    @Override
    public Result<PageResult<List<ScItemUnitResponse>>> pageQueryScItemUnitList(QueryScItemUnitRequest request) {
        Result<List<ScItemUnitSDO>> pageResultResult = scItemUnitQueryService.pageQueryScItemUnitList(request);
        List<ScItemUnitSDO> content = pageResultResult.getResult();
        List<ScItemUnitResponse> collect = content.stream().map(scItemUnitSDO -> {
            ScItemUnitResponse scItemUnitResponse = new ScItemUnitResponse();
            BeanUtils.copyProperties(scItemUnitSDO, scItemUnitResponse);
            return scItemUnitResponse;
        }).collect(Collectors.toList());
        return Result.success(PageResult.of(collect,pageResultResult.getTotal()));
    }
}
